package com.aopeng.inter.impl;

import com.aopeng.entity.AopengExamination;
import com.aopeng.inter.WebServiceInter;
import com.aopeng.mapper.AopengExaminationMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

/**
 * 实现调用外部接口
 * @author <a href="mailto:juhui.chen@vtradex.com">陈局辉</a>
 * @since 2020-08-22 18:51
 */
@Service
@Component
@Configuration
@EnableScheduling
public class WebServiceInterImpl implements WebServiceInter {

    @Autowired
    public AopengExaminationMapper aopengExaminationMapper;

    @Override
    public String linkSubmitBaiDu(String PostUrl, Object[] Parameters) {
        if (null == PostUrl || null == Parameters || Parameters.length == 0) {
            return null;
        }
        String result = "";
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            //建立URL之间的连接
            URLConnection conn = new URL(PostUrl).openConnection();
            //设置通用的请求属性
            conn.setRequestProperty("Host", "data.zz.baidu.com");
            conn.setRequestProperty("User-Agent", "curl/7.12.1");
            conn.setRequestProperty("Content-Length", "83");
            conn.setRequestProperty("Content-Type", "text/plain");
            //发送POST请求必须设置如下两行
            conn.setDoInput(true);
            conn.setDoOutput(true);
            //获取conn对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            //发送请求参数
            String param = "";
            for (Object s : Parameters) {
                param += s.toString() + "\n";
            }
            out.print(param.trim());
            //进行输出流的缓冲
            out.flush();
            //通过BufferedReader输入流来读取Url的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送post请求出现异常！" + e);
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    @Override
    //@Scheduled(cron="0 15 0 * * ?")
    public void submitBaiDu() {
        List<AopengExamination> aopengExaminations = aopengExaminationMapper
            .selectList(new QueryWrapper<AopengExamination>().isNull("updater").last("limit 3000"));
        List<String> list = new ArrayList<>();
        AopengExamination aopengExamination = new AopengExamination();
        aopengExamination.setUpdater(1L);
        ArrayList<Long> id = new ArrayList<>();
        System.out.println(aopengExaminations.size());
        if (aopengExaminations!=null&&aopengExaminations.size()!=0){
            for (AopengExamination ae : aopengExaminations) {
                list.add("http://www.aopen100.com/client/content/"+ae.getId()+".html");
                id.add(ae.getId());
            }
            //网站的服务器连接
            String url = "http://data.zz.baidu.com/urls?site=www.aopen100.com&token=Z29W686o5NwEe7Ip";
            Object[] objects = list.toArray();
            //执行推送方法
            String json = linkSubmitBaiDu(url, objects);
            //打印推送结果
            System.out.println("结果是" + json);
            aopengExaminationMapper.update(aopengExamination, new QueryWrapper<AopengExamination>().in("id", id));
        }
    }

}
